package com.zx.stock.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zx.stock.common.Const;
import com.zx.stock.entity.Performance;
import com.zx.stock.entity.PerformanceData;
import com.zx.stock.mapper.PerformanceDataMapper;
import com.zx.stock.utils.HttpsUtil;
import com.zx.stock.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PerformanceThread implements Runnable{

    PerformanceData entity;
    PerformanceDataMapper performanceDataMapper;

    public PerformanceThread(PerformanceData stockList, PerformanceDataMapper stockAgencyMapper){
        this.entity = stockList;
        this.performanceDataMapper = stockAgencyMapper;
    }

    @Override
    public void run(){

        String jsonStr = HttpsUtil.doGet(entity.getUrl());

        if (!StringUtil.isEmpty(jsonStr)){
            log.info("请求结果:{},成功",entity.getPage());

            String content = convertDataResult(jsonStr);

            if (!StringUtil.isEmpty(content)){
                log.info("请求结果:{},解析成功",entity.getPage());
                PerformanceData item = new PerformanceData();
                item.setStatus(Const.Y);
                item.setContent(content);
                item.setId(entity.getId());
                performanceDataMapper.updateDynamic(item);
            }else{
                log.error("请求结果:{},解析失败",entity.getPage());
            }
            //Performance


        }else{
            log.error("请求结果:{},失败",entity.getPage());
        }
    }

    private String convertDataResult(String jsonStr){


        DecimalFormat decimalFormat=new DecimalFormat(".00");

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        if (jsonObject.containsKey("result")){

            JSONObject rsObject = jsonObject.getJSONObject("result");
            if (rsObject.containsKey("data")){


                List<Performance> itemList = new ArrayList<>();
                JSONArray array=rsObject.getJSONArray("data");
                for(int i=0;i<array.size();i++){
                    JSONObject job = array.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象

                    Performance item = new Performance();
                    item.setNoticeDate(entity.getNoticeDate());
                    item.setCode(job.getString("SECURITY_CODE"));
                    item.setName(job.getString("SECURITY_NAME_ABBR"));
                    item.setType(job.getString("PREDICT_TYPE"));
                    item.setSummary(job.getString("PREDICT_CONTENT"));

                    //job.getString("ADD_AMP_LOWER") job.getString("ADD_AMP_UPPER")

                    //log.info("ADD_AMP_LOWER = {},ADD_AMP_UPPER={}",job.getString("ADD_AMP_LOWER"),job.getString("ADD_AMP_UPPER"));

                    if (!StringUtil.isEmpty(job.getString("ADD_AMP_LOWER")) && !StringUtil.isEmpty(job.getString("ADD_AMP_UPPER"))){
                        String a = decimalFormat.format(job.getFloat("ADD_AMP_LOWER")/100000000.0);
                        String b = decimalFormat.format(job.getFloat("ADD_AMP_UPPER")/100000000.0);
                        item.setChangeProfit(String.format("%s亿~%s亿",a,b));
                    }else{

                    }


                    String rate1 = job.getString("ADD_AMP_LOWER")!=null ? job.getString("ADD_AMP_LOWER"):"0";
                    String rate2 = job.getString("ADD_AMP_UPPER")!=null ? job.getString("ADD_AMP_UPPER"):"0";

                    item.setMoneyProfit(rate1+"%~"+rate2);
                    item.setAmDate(job.getDate("REPORT_DATE"));

                    itemList.add(item);
                }

                return JSON.toJSONString(itemList);
            }
        }
        return null;
    }
}
